<?php
class SectionsModel extends Model
{
    function _construct()
    {
        parent::__construct();
    }

    public function loadSection()
    {
        $section = null;
        $section = $this->getSection();

        $this->loadView('sections');
        $this->_views['sections']->setBlockData($section);
        $this->_views['sections']->render();
    }

    public function getSections($subjectId)
    {
        $db = DataProvider::getInstance();
        $session = Session::getInstance();
        $this->loadTable('classes');
        $data = array();
        $sql = "select * from subjects s";
        
        $data['subjectList'] = $db->_query($sql);
        if($subjectId != NULL)
        {
            $this->loadTable('sections');
            $condition = array('subjectid' => $subjectId);
            $data['sectionList'] = $this->tables['sections']->get($condition);
        }
        $data['currentSubject'] = $subjectId;
        return $data;
    }

    public function add($subjectid, $name, $weight)
    {


        $this->loadTable('sections');
        if ($this->checkName($name))
        {

            $message = "Đã tồn tại chương. Nhập vào chương khác!";
            return $message;
        }
        else
        {
            $sectionInfo = array('subjectid' => $subjectid, 'name' => $name, 'weight' => $weight);
            $sectionObj = new SectionsTable($sectionInfo);
            $sectionObj->insert();
        }

    }


    private function checkName($name)
    {
        $this->loadTable('sections');
        $condition = array('name' => $name);
        $sectionObj = new SectionsTable();
        $name_check = $sectionObj->get($condition)->getAll();
        if (count($name_check) > 0) {
            return true;
        }
        else return false;
    }


    public function edit($id, $name, $weight)
    {

        $this->loadTable('sections');
        $sectionInfo = array('name' => $name, 'weight' => $weight);
        $sectionObj = new SectionsTable($sectionInfo);
        $condition = array('id' => $id);
        $sectionObj->update($condition);
    }

    public function remove($id)
    {

        $this->loadTable('sections');
        $sectionInfo = array('id' => $id);
        $sectionObj = new SectionsTable($sectionInfo);
        $condition = array('id' => $id);
        $sectionObj->remove($condition);
    }

}


?>
